package com.ydd.jpush;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.ydd.moudle.main.HomeActivity;
import com.ydd.moudle.me.LoginActivity;
import com.ydd.util.user.UserManager;

import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by tao on 17-3-18.
 * 用来接受极光SDK推送给APP的消息
 */

public class JPushReceiver extends BroadcastReceiver {
    private static final String TAG = JPushReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        //不需要跳轉

        if (intent.getAction().equals(JPushInterface.ACTION_NOTIFICATION_RECEIVED)) {

        } else if (intent.getAction().equals(JPushInterface.ACTION_NOTIFICATION_OPENED)) {
            //需要跳轉
            Gson gson = new Gson();
            PushMessage message = gson.fromJson(bundle.getString(JPushInterface.EXTRA_EXTRA)
                    , PushMessage.class);

            //應用已經啓動
            if (getCurrentTask(context)) {
                Intent pushIntent = new Intent();
                pushIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                pushIntent.putExtra("pushMessage", message);

                //需要登錄但沒有登錄
                if (message != null && message.messageType.equals("2") &&
                        !UserManager.getInstance().hasLogined()) {
                    pushIntent.setClass(context, LoginActivity.class);
                    pushIntent.putExtra("fromPush", true);
                } else {
                    //不需要登陆或者已经登陆的Case,直接跳转到内容显示页面
                    pushIntent.setClass(context, PushMessageActivity.class);
                }
                context.startActivity(pushIntent);
            } else {

                Intent mainIntent = new Intent(context, HomeActivity.class);
                mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                if (message != null && message.messageType.equals("2")) {
                    Intent loginIntent = new Intent(context, LoginActivity.class);
                    loginIntent.putExtra("fromPush", true);
                    loginIntent.putExtra("pushMessage", message);

                    context.startActivities(new Intent[]{mainIntent, loginIntent});
                } else {
                    Intent pushIntent = new Intent(context, PushMessageActivity.class);
                    pushIntent.putExtra("pushMessage", message);

                    context.startActivities(new Intent[]{mainIntent, pushIntent});
                }
            }
        }
    }

    /**
     * 这个是真正的获取指定包名的应用程序是否在运行(无论前台还是后台)
     *
     * @return
     */
    private boolean getCurrentTask(Context context) {

        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> appProcessInfos = activityManager.getRunningTasks(50);
        for (ActivityManager.RunningTaskInfo process : appProcessInfos) {

            if (process.baseActivity.getPackageName().equals(context.getPackageName())
                    || process.topActivity.getPackageName().equals(context.getPackageName())) {

                return true;
            }
        }
        return false;
    }
}
